<template>
  <div class="container">
    <div class="app-container">
      <el-tree default-expand-all :data="depts" :props="defaultProps" :expand-on-click-node="false">
        <template v-slot="{ data }">
          <el-row style="width:100%;height:40px" type="flex" justify="space-between" align="middle">
            <el-col :span="20">{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="tree-manager">{{ data.managerName }}</span>
              <!-- $event 实参 表示类型 -->
              <!-- 在el-dropdown自定义组件中绑定点击事件有两种用法 -->
              <!-- 一种是使用@click='OperateDept('add')',但是因为是自定义组件不支持原生事件，所以在click后.native -->
              <!-- 另一种是使用自定义组件自带的，@command="OperateDept" 在el-dropdown写  command="add"在el-dropdown-item写 -->
              <!-- data.id 是data对象拿到的id值 -->
              <el-dropdown @command="OperateDept($event,data.id)">
                <!-- 显示区域内容 -->
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <!-- 下拉菜单选项 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
      <!-- 添加弹层 -->
      <!-- <AddDialog :visible="showDialog" @update:visible="showDialog = false" /> -->
      <!-- :visible + @update:visible 等于 :visible.sync-->
      <AddDialog ref="addDeptRef" :current-node-id="currentNodeId" :visible.sync="showDialog" @updateDepartment="getDepartment" />
    </div>
  </div>
</template>
<script>
import { getDepartmentApi, delDepartmentApi } from '@/api/department'
import { transObjToTree } from '@/utils'
import AddDialog from './components/addDialog.vue'

export default {
  name: 'Department',
  components: {
    AddDialog
  },
  data() {
    return {
      depts: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      showDialog: false,
      // 这是父向子传递的pid值
      currentNodeId: null
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      const res = await getDepartmentApi()
      console.log(res)
      this.depts = transObjToTree(res, 0)
    },
    OperateDept(type, pid) {
      // 新增
      if (type === 'add') {
        this.showDialog = true
        console.log(pid)
        // 这里拿到pid的值
        this.currentNodeId = pid
      }
      // 编辑
      if (type === 'edit') {
        // 点击编辑部门后开启弹层
        this.showDialog = true
        // this.currentNodeId = pid
        // 这是父组件去拿子组件中的请求数据方法
        this.$refs.addDeptRef.getDepartmentDetai(pid)
      }
      // 删除
      if (type === 'del') {
        this.$confirm('您确定要修改该部门吗?').then(async() => {
          await delDepartmentApi(pid)
          this.$message.success('删除成功~~~')
          this.getDepartment()
        })
      }
    }
  }
}
</script>

<style scoped lang="scss">
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.tree-manager {
  width: 80px;
  display: inline-block;
  margin: 10px;
}
</style>
